package com.zw.user.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.object.result.Result;
import com.zw.object.system.SysUser;
import com.zw.object.vo.SysUserQueryVo;
import com.zw.user.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.jfr.Percentage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zw
 * @since 2022-12-06
 */
@Api(tags ="用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
@CrossOrigin
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     *                          用户分页列表接口
     * @param page              页码
     * @param limit             当页显示大小
     * @param sysUserQueryVo    参数实体类
     * @return Result
     */
    @ApiOperation("获取用户分页列表")
    @GetMapping("/{page}/{limit}")
    public Result sysUserListPage(@PathVariable Long page,
                                  @PathVariable Long limit,
                                  SysUserQueryVo sysUserQueryVo){
        IPage<SysUser> iPage = new Page<SysUser>(page, limit);
        return sysUserService.sysUserListPage(iPage,sysUserQueryVo);
    }

    /**
     *              根据用户ID获取用户接口
     * @param id    用户ID
     * @return      Result
     */
    @ApiOperation("根据ID获取用户信息")
    @GetMapping("/get/{id}")
    public Result getUserByUserId(@PathVariable String id){
        return sysUserService.getUserByUserId(id);
    }

    /**
     *                  用户保存接口
     * @param sysUser   用户参数实体对象
     * @return          Result
     */
    @PreAuthorize("hasAnyAuthority('bnt.sysUser.add')")
    @ApiOperation("保存用户")
    @PostMapping("/save")
    public Result Save(@RequestBody SysUser sysUser){
        return sysUserService.saveSysUser(sysUser);
    }

    /**
     *                  更新用户接口
     * @param sysUser   更新用户参数实体类
     * @return          Result
     */
    @PreAuthorize("hasAnyAuthority('bnt.sysUser.update')")
    @ApiOperation("更新用户")
    @PostMapping("/update")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        return sysUserService.updateSysUSer(sysUser);
    }

    /**
     *              根据用户ID删除用户接口
     * @param id    用户ID
     * @return      Result
     */
    @PreAuthorize("hasAnyAuthority('bnt.sysUser.remove')")
    @ApiOperation("根据ID删除用户")
    @DeleteMapping("/remove/{id}")
    public Result removeSysUserById(@PathVariable String id){
        return sysUserService.removeSysUserById(id);
    }

    /**
     *              批量删除用户接口
     * @param ids   用户id集合
     * @return      Result
     */
    @PreAuthorize("hasAuthority('bnt.sysUser.batchRemove')")
    @ApiOperation("批量删除用户")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveSysUser(@RequestBody List<String> ids){
        return sysUserService.batchRemove(ids);
    }

    /**
     *                  根据用户ID修改用户状态接口
     * @param id        用户ID
     * @param status    用户状态
     * @return          Result
     */
    @ApiOperation("更新用户状态")
    @PreAuthorize("hasAuthority('bnt.sysUser.statusUpdate')")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateUserStatus(@PathVariable String id,@PathVariable Integer status){
        return sysUserService.updateUserStatus(id,status);
    }
}

